using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

using NUnit.Framework;

using NSimpleDB.Service.VistaDb.QueryProcessing.Compiler;

namespace test.Service.VistaDb.Whitebox
{
    [TestFixture]
    public class test_Query_Tokenizer
    {
        [Test]
        public void testRead()
        {
            QueryTokenizer t;

            t = new QueryTokenizer("");
            Assert.AreEqual(QueryTokensTypes.EOQ, t.Read());
            Assert.AreEqual(QueryTokensTypes.EOQ, t.Read());

            t = new QueryTokenizer("   ");
            Assert.AreEqual(QueryTokensTypes.EOQ, t.Read());

            t = new QueryTokenizer("intersection [");
            Assert.AreEqual(QueryTokensTypes.IntersectionOp, t.Read());
            Assert.AreEqual(QueryTokensTypes.OpenPredicate, t.Read());
            Assert.AreEqual(QueryTokensTypes.EOQ, t.Read());

            t = new QueryTokenizer("x");
            Assert.AreEqual(QueryTokensTypes.ERROR, t.Read());
            Assert.AreEqual(QueryTokensTypes.EOQ, t.Read());
        }


        [Test]
        public void testCurrentTokenTypeAndTextAndPosition()
        {
            QueryTokenizer t;

            // test empty query
            t = new QueryTokenizer("");
            t.Read();
            Assert.AreEqual(QueryTokensTypes.EOQ, t.CurrentTokenType);
            Assert.AreEqual("", t.CurrentTokenText);
            Assert.AreEqual(-1, t.CurrentTokenPosition);

            // test a couple of different tokens
            t = new QueryTokenizer("intersection [ >= 'attribute' x");
            t.Read();
            Assert.AreEqual(QueryTokensTypes.IntersectionOp, t.CurrentTokenType);
            Assert.AreEqual("intersection", t.CurrentTokenText);
            Assert.AreEqual(0, t.CurrentTokenPosition);

            t.Read();
            Assert.AreEqual(QueryTokensTypes.OpenPredicate, t.CurrentTokenType);
            Assert.AreEqual("[", t.CurrentTokenText);
            Assert.AreEqual(13, t.CurrentTokenPosition);

            t.Read();
            Assert.AreEqual(QueryTokensTypes.LargerOrEqualOp, t.CurrentTokenType);
            Assert.AreEqual(">=", t.CurrentTokenText);
            Assert.AreEqual(15, t.CurrentTokenPosition);

            t.Read();
            Assert.AreEqual(QueryTokensTypes.AttributeNameOrValue, t.CurrentTokenType);
            Assert.AreEqual("'attribute'", t.CurrentTokenText);
            Assert.AreEqual(18, t.CurrentTokenPosition);

            t.Read();
            Assert.AreEqual(QueryTokensTypes.ERROR, t.CurrentTokenType);
            Assert.AreEqual("x", t.CurrentTokenText);
            Assert.AreEqual(30, t.CurrentTokenPosition);

            t.Read();
            Assert.AreEqual(QueryTokensTypes.EOQ, t.CurrentTokenType);
            Assert.AreEqual("", t.CurrentTokenText);
            Assert.AreEqual(-1, t.CurrentTokenPosition);

            // test empty attribute value/name
            t = new QueryTokenizer("''");
            t.Read();
            Assert.AreEqual(QueryTokensTypes.AttributeNameOrValue, t.CurrentTokenType);

            // test case insensitiveness
            t = new QueryTokenizer("UNION");
            t.Read();
            Assert.AreEqual(QueryTokensTypes.UnionOp, t.CurrentTokenType);
        }



    }
}
